﻿Public Class FrmNhomHangHoa
    Dim provider As New DataProvider
    Dim flag As String = 0
    Function LoadDuLieu()
        dtgnhomhanghoa.DataSource = New DataTable
        Dim sql As String
        sql = "SELECT * FROM NhomHangHoa"
        Dim dt As New DataTable
        dt = provider.ExecuteQuery(sql)
        dtgnhomhanghoa.DataSource = dt
        Enable(False)
        btnthem.Enabled = True
        btnluu.Enabled = False
        btnsua.Enabled = True
        btnxoa.Enabled = True
        dtgnhomhanghoa.Enabled = True
        btnhuy.Enabled = False


    End Function
    Private Sub btnthem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthem.Click
        flag = 1
        Enable(True)
        btnsua.Enabled = False
        btnxoa.Enabled = False
        btnthem.Enabled = False
        dtgnhomhanghoa.Enabled = False
        txtmanhomhh.Text = ""
        txttennhomhh.Text = ""
        btnhuy.Enabled = True
    End Sub

    Private Sub btnxoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnxoa.Click
        Try
            Dim row As Integer = dtgnhomhanghoa.CurrentCell.RowIndex
            Dim maNhomHangHoa As String
            maNhomHangHoa = dtgnhomhanghoa.Rows(row).Cells(0).Value
            Dim sql As String
            sql = " DELETE FROM NhomHangHoa WHERE maNhomHangHoa = '" + maNhomHangHoa + "'"
            provider.ExecuteNonQuery(sql)
            LoadDuLieu()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnsua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsua.Click
        btnhuy.Enabled = False
        If txtmanhomhh.Text = "" Then
            MessageBox.Show("Chọn 1 nhóm hàng hóa để sửa ")
        Else
            flag = 2
            Enable(True)
            btnthem.Enabled = False
            btnxoa.Enabled = False
            btnsua.Enabled = False
            txtmanhomhh.Enabled = False

        End If
    End Sub

    Private Sub btnluu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnluu.Click
        Try

            If flag = 1 Then
                Dim maNhomHangHoa As String
                Dim tenNhomHangHoa As String
                maNhomHangHoa = txtmanhomhh.Text
                tenNhomHangHoa = txttennhomhh.Text
                Dim sql As String
                sql = "SELECT * FROM NhomHangHoa WHERE maNhomHangHoa='" + maNhomHangHoa + "'"
                Dim dt As New DataTable
                dt = provider.ExecuteQuery(sql)

                If dt.Rows.Count > 0 Then
                    MessageBox.Show("Mã nhóm hàng hóa Trùng")
                    txtmanhomhh.Focus()
                Else
                    sql = "INSERT INTO NhomHangHoa values(N'" + maNhomHangHoa + "',N'" + tenNhomHangHoa + "')"
                    provider.ExecuteNonQuery(sql)
                    LoadDuLieu()
                End If


            ElseIf flag = 2 Then

                Dim maNhomHangHoa As String
                Dim tenNhomHangHoa As String
                maNhomHangHoa = txtmanhomhh.Text
                tenNhomHangHoa = txttennhomhh.Text
                Dim sql As String
                sql = "UPDATE NhomHangHoa set maNhomHangHoa = '" + maNhomHangHoa + "', tenNhomHangHoa= N'" + tenNhomHangHoa + "' WHERE maNhomHangHoa = '" + maNhomHangHoa + "'"
                provider.ExecuteNonQuery(sql)
                LoadDuLieu()
            End If

        Catch ex As Exception
            MessageBox.Show("Lỗi")
        End Try
    End Sub
    Private Sub Enable(ByVal i As Boolean)
        txtmanhomhh.Enabled = i
        txttennhomhh.Enabled = i
        btnluu.Enabled = i

    End Sub
    Private Sub btnhuy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnhuy.Click
        LoadDuLieu()

    End Sub

    Private Sub FrmNhomHangHoa_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadDuLieu()
    End Sub

    Private Sub dtgnhomhanghoa_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
        Try
            Dim row As Integer = dtgnhomhanghoa.CurrentCell.RowIndex
            txtmanhomhh.Text = dtgnhomhanghoa.Rows(row).Cells(0).Value
            txttennhomhh.Text = dtgnhomhanghoa.Rows(row).Cells(1).Value
        Catch ex As Exception

        End Try
    End Sub
End Class